<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>inputhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_inputhandler.js.html">inputhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An object that encapsulates text changed events for textareas
<a name="line17"></a> * and input element of type text and password. The event occurs after the value
<a name="line18"></a> * has been changed. The event does not occur if value was changed
<a name="line19"></a> * programmatically.&lt;br&gt;
<a name="line20"></a> * &lt;br&gt;
<a name="line21"></a> * Note: this does not guarantee the correctness of {@code keyCode} or
<a name="line22"></a> * {@code charCode}, or attempt to unify them across browsers. See
<a name="line23"></a> * {@code goog.events.KeyHandler} for that functionality.&lt;br&gt;
<a name="line24"></a> * &lt;br&gt;
<a name="line25"></a> * Known issues:
<a name="line26"></a> * &lt;ul&gt;
<a name="line27"></a> * &lt;li&gt;Does not trigger for drop events on Opera due to browser bug.
<a name="line28"></a> * &lt;li&gt;IE doesn&#39;t have native support for input event. WebKit before version 531
<a name="line29"></a> *     doesn&#39;t have support for textareas. For those browsers an emulation mode
<a name="line30"></a> *     based on key, clipboard and drop events is used. Thus this event won&#39;t
<a name="line31"></a> *     trigger in emulation mode if text was modified by context menu commands
<a name="line32"></a> *     such as &#39;Undo&#39; and &#39;Delete&#39;.
<a name="line33"></a> * &lt;/ul&gt;
<a name="line34"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line35"></a> * @see ../demos/inputhandler.html
<a name="line36"></a> */
<a name="line37"></a>
<a name="line38"></a>goog.provide(&#39;goog.events.InputHandler&#39;);
<a name="line39"></a>goog.provide(&#39;goog.events.InputHandler.EventType&#39;);
<a name="line40"></a>
<a name="line41"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line42"></a>goog.require(&#39;goog.dom&#39;);
<a name="line43"></a>goog.require(&#39;goog.events&#39;);
<a name="line44"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line45"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line46"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line47"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line48"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * This event handler will dispatch events when the user types into a text
<a name="line54"></a> * input, password input or a textarea
<a name="line55"></a> * @param {Element} element  The element that you want to listen for input
<a name="line56"></a> *     events on.
<a name="line57"></a> * @constructor
<a name="line58"></a> * @extends {goog.events.EventTarget}
<a name="line59"></a> */
<a name="line60"></a>goog.events.InputHandler = function(element) {
<a name="line61"></a>  goog.base(this);
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * The element that you want to listen for input events on.
<a name="line65"></a>   * @type {Element}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.element_ = element;
<a name="line69"></a>
<a name="line70"></a>  // Determine whether input event should be emulated.
<a name="line71"></a>  // IE8 doesn&#39;t support input events. We could use property change events but
<a name="line72"></a>  // they are broken in many ways:
<a name="line73"></a>  // - Fire even if value was changed programmatically.
<a name="line74"></a>  // - Aren&#39;t always delivered. For example, if you change value or even width
<a name="line75"></a>  //   of input programmatically, next value change made by user won&#39;t fire an
<a name="line76"></a>  //   event.
<a name="line77"></a>  // IE9 supports input events when characters are inserted, but not deleted.
<a name="line78"></a>  // WebKit before version 531 did not support input events for textareas.
<a name="line79"></a>  var emulateInputEvents = goog.userAgent.IE ||
<a name="line80"></a>      (goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersion(&#39;531&#39;) &amp;&amp;
<a name="line81"></a>          element.tagName == &#39;TEXTAREA&#39;);
<a name="line82"></a>
<a name="line83"></a>  /**
<a name="line84"></a>   * @type {goog.events.EventHandler}
<a name="line85"></a>   * @private
<a name="line86"></a>   */
<a name="line87"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line88"></a>
<a name="line89"></a>  // Even if input event emulation is enabled, still listen for input events
<a name="line90"></a>  // since they may be partially supported by the browser (such as IE9).
<a name="line91"></a>  // If the input event does fire, we will be able to dispatch synchronously.
<a name="line92"></a>  // (InputHandler events being asynchronous for IE is a common issue for
<a name="line93"></a>  // cases like auto-grow textareas where they result in a quick flash of
<a name="line94"></a>  // scrollbars between the textarea content growing and it being resized to
<a name="line95"></a>  // fit.)
<a name="line96"></a>  this.eventHandler_.listen(
<a name="line97"></a>      this.element_,
<a name="line98"></a>      emulateInputEvents ?
<a name="line99"></a>          [&#39;keydown&#39;, &#39;paste&#39;, &#39;cut&#39;, &#39;drop&#39;, &#39;input&#39;] :
<a name="line100"></a>          &#39;input&#39;,
<a name="line101"></a>      this);
<a name="line102"></a>};
<a name="line103"></a>goog.inherits(goog.events.InputHandler, goog.events.EventTarget);
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Enum type for the events fired by the input handler
<a name="line108"></a> * @enum {string}
<a name="line109"></a> */
<a name="line110"></a>goog.events.InputHandler.EventType = {
<a name="line111"></a>  INPUT: &#39;input&#39;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Id of a timer used to postpone firing input event in emulation mode.
<a name="line117"></a> * @type {?number}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.events.InputHandler.prototype.timer_ = null;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * This handles the underlying events and dispatches a new event as needed.
<a name="line125"></a> * @param {goog.events.BrowserEvent} e The underlying browser event.
<a name="line126"></a> */
<a name="line127"></a>goog.events.InputHandler.prototype.handleEvent = function(e) {
<a name="line128"></a>  if (e.type == &#39;input&#39;) {
<a name="line129"></a>    // This event happens after all the other events we listen to, so cancel
<a name="line130"></a>    // an asynchronous event dispatch if we have it queued up.  Otherwise, we
<a name="line131"></a>    // will end up firing an extra event.
<a name="line132"></a>    this.cancelTimerIfSet_();
<a name="line133"></a>
<a name="line134"></a>    // Unlike other browsers, Opera fires an extra input event when an element
<a name="line135"></a>    // is blurred after the user has input into it. Since Opera doesn&#39;t fire
<a name="line136"></a>    // input event on drop, it&#39;s enough to check whether element still has focus
<a name="line137"></a>    // to suppress bogus notification.
<a name="line138"></a>    if (!goog.userAgent.OPERA || this.element_ ==
<a name="line139"></a>        goog.dom.getOwnerDocument(this.element_).activeElement) {
<a name="line140"></a>      this.dispatchEvent(this.createInputEvent_(e));
<a name="line141"></a>    }
<a name="line142"></a>  } else {
<a name="line143"></a>    // Filter out key events that don&#39;t modify text.
<a name="line144"></a>    if (e.type == &#39;keydown&#39; &amp;&amp;
<a name="line145"></a>        !goog.events.KeyCodes.isTextModifyingKeyEvent(e)) {
<a name="line146"></a>      return;
<a name="line147"></a>    }
<a name="line148"></a>
<a name="line149"></a>    // It is still possible that pressed key won&#39;t modify the value of an
<a name="line150"></a>    // element. Storing old value will help us to detect modification but is
<a name="line151"></a>    // also a little bit dangerous. If value is changed programmatically in
<a name="line152"></a>    // another key down handler, we will detect it as user-initiated change.
<a name="line153"></a>    var valueBeforeKey = e.type == &#39;keydown&#39; ? this.element_.value : null;
<a name="line154"></a>
<a name="line155"></a>    // In IE on XP, IME the element&#39;s value has already changed when we get
<a name="line156"></a>    // keydown events when the user is using an IME. In this case, we can&#39;t
<a name="line157"></a>    // check the current value normally, so we assume that it&#39;s a modifying key
<a name="line158"></a>    // event. This means that ENTER when used to commit will fire a spurious
<a name="line159"></a>    // input event, but it&#39;s better to have a false positive than let some input
<a name="line160"></a>    // slip through the cracks.
<a name="line161"></a>    if (goog.userAgent.IE &amp;&amp; e.keyCode == goog.events.KeyCodes.WIN_IME) {
<a name="line162"></a>      valueBeforeKey = null;
<a name="line163"></a>    }
<a name="line164"></a>
<a name="line165"></a>    // Create an input event now, because when we fire it on timer, the
<a name="line166"></a>    // underlying event will already be disposed.
<a name="line167"></a>    var inputEvent = this.createInputEvent_(e);
<a name="line168"></a>
<a name="line169"></a>    // Since key down, paste, cut and drop events are fired before actual value
<a name="line170"></a>    // of the element has changed, we need to postpone dispatching input event
<a name="line171"></a>    // until value is updated.
<a name="line172"></a>    this.cancelTimerIfSet_();
<a name="line173"></a>    this.timer_ = goog.Timer.callOnce(function() {
<a name="line174"></a>      this.timer_ = null;
<a name="line175"></a>      if (this.element_.value != valueBeforeKey) {
<a name="line176"></a>        this.dispatchEvent(inputEvent);
<a name="line177"></a>      }
<a name="line178"></a>    }, 0, this);
<a name="line179"></a>  }
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Cancels timer if it is set, does nothing otherwise.
<a name="line185"></a> * @private
<a name="line186"></a> */
<a name="line187"></a>goog.events.InputHandler.prototype.cancelTimerIfSet_ = function() {
<a name="line188"></a>  if (this.timer_ != null) {
<a name="line189"></a>    goog.Timer.clear(this.timer_);
<a name="line190"></a>    this.timer_ = null;
<a name="line191"></a>  }
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Creates an input event from the browser event.
<a name="line197"></a> * @param {goog.events.BrowserEvent} be A browser event.
<a name="line198"></a> * @return {goog.events.BrowserEvent} An input event.
<a name="line199"></a> * @private
<a name="line200"></a> */
<a name="line201"></a>goog.events.InputHandler.prototype.createInputEvent_ = function(be) {
<a name="line202"></a>  var e = new goog.events.BrowserEvent(be.getBrowserEvent());
<a name="line203"></a>  e.type = goog.events.InputHandler.EventType.INPUT;
<a name="line204"></a>  return e;
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/** @override */
<a name="line209"></a>goog.events.InputHandler.prototype.disposeInternal = function() {
<a name="line210"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line211"></a>  this.eventHandler_.dispose();
<a name="line212"></a>  this.cancelTimerIfSet_();
<a name="line213"></a>  delete this.element_;
<a name="line214"></a>};
</pre>


</body>
</html>
